﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;
using System.Messaging;
using OpenComposite.EII.Designer.Workflow.Forms;

namespace OpenComposite.EII.RepositoryUIs
{
	public partial class UCMessageQueue : RepositoryItemUI
	{
		public UCMessageQueue(IServiceProvider serviceProcider)
			: base(serviceProcider)
		{
			InitializeComponent();
		}

		MessageQueueEx _messageQueue = null;
		public override IRepositoryItem RepositoryItem
		{
			get
			{
				if ( _messageQueue != null ) {
					save();
				}
				return _messageQueue;
			}
			set
			{
				_messageQueue = value as MessageQueueEx;
				if ( value != null ) {
					load();
				}
			}
		}

		public override string TypeText
		{
			get
			{
				return "Message Queue";
			}
		}

		public override string TypeDescription
		{
			get
			{
				return "Message Queues used for communication with non web service interfaces.";
			}
		}

		private void load()
		{
			txtName.Text = MessageQueue.Name;
			txtPath.Text = MessageQueue.Path;
			txtDescription.Text = MessageQueue.Description;
		}

		private void save()
		{
			MessageQueue.Name = txtName.Text;
			MessageQueue.Path = txtPath.Text;
			MessageQueue.Description = txtDescription.Text;
			MessageQueue.Save();
		}

		internal MessageQueueEx MessageQueue
		{
			get { return _messageQueue; }
			set { _messageQueue = value; }
		}

		private void btnDiscover_Click(object sender, EventArgs e)
		{
			FormMsmqConfig form = new FormMsmqConfig();
			form.MessageQueuePath = txtPath.Text;
			if ( form.ShowDialog() == DialogResult.OK ) {
				txtPath.Text = form.MessageQueuePath;
			}
		}
	}
}
